import allure
import pytest
from selenium import webdriver

@pytest.fixture(scope="session")
def driver():
    # 将其设置为全部变量
    global driver
    driver = webdriver.Edge()
    url = "http://192.168.1.5/ecshop/admin/privilege.php?act=login"
    driver.get(url=url)
    yield driver
    # 环境恢复
    driver.quit()


# 钩子函数
## 钩子函数，获取结果
@pytest.hookimpl(hookwrapper=True,tryfirst=True)
def pytest_runtest_makereport(item,call):
# 获取钩子函数的结果
    out = yield
    # 获取测试报告
    report = out.get_result()
    # 脚本执行失败，截图
    if report.when == 'call' and report.outcome == "failed":
        #把截图保存到allure测试报告中
        allure.attach(driver.get_screenshot_as_png(),"失败截图",attachment_type=allure.attachment_type.PNG)
